package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.clearweed.ClearWeedGood;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/6.
 */

public class ClearWeedExchangeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;
    private List<ClearWeedGood> list;
    private JSONObject storeInfo;
    private int footer_state=0;
    private OnItemClickListener itemClickListener;

    //普通布局
    static final int TYPE_ITEM = 0;
    //底部布局
    static final int TYPE_FOOTER = 1;

    //正在加载更多
    static final int LOAD_MORE=1;

    //没有更多
    static final int NO_MORE=2;

    //初始状态
    static final int INIT=0;

    public ClearWeedExchangeAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public void setList(List<ClearWeedGood> list,JSONObject storeInfo) {
        this.list = list;
        this.storeInfo=storeInfo;
        notifyDataSetChanged();
    }

    public void setFooter_state(int footer_state) {
        this.footer_state = footer_state;
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = View.inflate(context, R.layout.recycle_exchange_layout, null);
            RecycleViewHolder viewHolder = new RecycleViewHolder(view);
            return viewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View view = View.inflate(context, R.layout.recycle_footer_layout, null);
            FooterViewHolder footerViewHolder = new FooterViewHolder(view);
            return footerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecycleViewHolder){
            ClearWeedGood clearWeedGood = list.get(position);
            
            if(null!=clearWeedGood&&!"".equals(clearWeedGood)){
                Glide.with(context)
                        .load(Url.IMG_URL+clearWeedGood.getThumbnail())
                        .asBitmap()
                        .placeholder(R.mipmap.hicc)
                        .centerCrop()
                        .into(((RecycleViewHolder) holder).exchangeItemImage);
                      /*  .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                ((RecycleViewHolder) holder).exchangeItemImage.setImageBitmap(resource);
                            }
                        });*/

                      if(null!=clearWeedGood.getPostage()&&"0".equals(clearWeedGood.getPostage())){
                          ((RecycleViewHolder) holder).exchangePosetFree.setVisibility(View.VISIBLE);
                      }else{
                          ((RecycleViewHolder) holder).exchangePosetFree.setVisibility(View.GONE);
                      }
                if(null!=clearWeedGood.getName()&&!"".equals(clearWeedGood.getName())){
                    ((RecycleViewHolder) holder).exchangeItemName.setText(clearWeedGood.getName()+"");
                }else{
                    ((RecycleViewHolder) holder).exchangeItemName.setText("");
                }
                if(null!=clearWeedGood.getMarketprice()&&!"".equals(clearWeedGood.getMarketprice())){
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setText("原价："+clearWeedGood.getMarketprice()+"元");
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                }else{
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setText("");
                }
                if(null!=clearWeedGood.getPurchasingnum()&&!"".equals(clearWeedGood.getPurchasingnum())){
                    ((RecycleViewHolder) holder).exchangeItemHasExchange.setText(clearWeedGood.getPurchasingnum()+"人已兑");
                }else{
                    ((RecycleViewHolder) holder).exchangeItemHasExchange.setText("0人已兑");
                }
                if(null!=clearWeedGood.getInnerprice()&&!"".equals(clearWeedGood.getInnerprice())){
                    ((RecycleViewHolder) holder).exchangeItemClearWeed.setText(clearWeedGood.getInnerprice()+"美豆");
                }else{
                    ((RecycleViewHolder) holder).exchangeItemClearWeed.setText("");
                }
                if(null!=clearWeedGood.getRedbeanprice()&&!"".equals(clearWeedGood.getRedbeanprice())){
                    ((RecycleViewHolder) holder).getExchangeItemRedBean.setText(" + "+clearWeedGood.getRedbeanprice()+"红豆");
                }else{
                    ((RecycleViewHolder) holder).getExchangeItemRedBean.setText("");
                }
                if(null!=clearWeedGood.getSid()&&!"".equals(clearWeedGood.getSid())){
                    JSONObject object=storeInfo.optJSONObject(clearWeedGood.getSid());
                    if(null!=object.optString("distance")&&!"".equals(object.optString("distance"))){
                        ((RecycleViewHolder) holder).exchangeItemDistance.setText("距您："+ DataUtil.formatData(Float.parseFloat(object.optString("distance"))*1.0/1000+"")+"km");
                    }else{
                        ((RecycleViewHolder) holder).exchangeItemDistance.setText("");
                    }
                    if(null!=object.optString("address")&&!"".equals(object.optString("address"))){
                        ((RecycleViewHolder) holder).exchangeItemAddress.setText("商家："+object.optString("address"));
                    }else{
                        ((RecycleViewHolder) holder).exchangeItemAddress.setText("");
                    }
                }else{
                    ((RecycleViewHolder) holder).exchangeItemDistance.setText("");
                    ((RecycleViewHolder) holder).exchangeItemAddress.setText("");
                }
            }
        }else if (holder instanceof FooterViewHolder){

            switch (footer_state){
                case LOAD_MORE://2
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.VISIBLE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);

                    ((FooterViewHolder) holder).recycleFootTv.setText("正在加载...");
                    break;
                case NO_MORE://1
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.GONE);

                    ((FooterViewHolder) holder).recycleFootTv.setText("已是全部数据了");
                    break;
                case INIT://上拉加载
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    break;
            }
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (position+1==getItemCount()){
            return TYPE_FOOTER;
        }else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list!=null?list.size()+1:0;
    }

    class RecycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.exchange_item_image)
        ImageView exchangeItemImage;
        @Bind(R.id.exchange_item_name)
        TextView exchangeItemName;
        @Bind(R.id.exchange_item_originalPrice)
        TextView exchangeItemOriginalPrice;
        @Bind(R.id.exchange_item_hasExchange)
        TextView exchangeItemHasExchange;
        @Bind(R.id.exchange_item_clearWeed)
        TextView exchangeItemClearWeed;
        @Bind(R.id.exchange_item_distance)
        TextView exchangeItemDistance;
        @Bind(R.id.exchange_item_address)
        TextView exchangeItemAddress;
        @Bind(R.id.exchange_item_redbean)
        TextView getExchangeItemRedBean;
        @Bind(R.id.exchange_postage_free)
        TextView exchangePosetFree;

        public RecycleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (itemClickListener != null) {
                itemClickListener.onClick(view,getAdapterPosition());
            }
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.recycle_tv_line1)
        TextView recycleTvLine1;
        @Bind(R.id.recycle_progressbar)
        ProgressBar recycleProgressbar;
        @Bind(R.id.recycle_foot_tv)
        TextView recycleFootTv;
        @Bind(R.id.recycle_tv_line2)
        TextView recycleTvLine2;
        @Bind(R.id.recycle_container_relative)
        RelativeLayout recycleContainerRelative;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public static interface OnItemClickListener{
        void onClick(View view,int position);
    }

}
